use rbatis::{rbdc::DateTime, RBatis};
use rbs::value::map::ValueMap;

#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Topic {
    pub id: Option<u64>,
    pub title: Option<String>,
    //类型，1:md,其它为:html
    pub style: Option<i32>,
    pub content: Option<String>,
    pub atime: Option<DateTime>,
    pub tag: Option<String>,
    pub sum: Option<String>,
    pub pic: Option<String>,
    pub ist: Option<u32>,
    pub num: Option<u32>,
    pub pass: Option<String>,
    pub hide: Option<u32>,
    pub lock: Option<u32>,
    pub tid: Option<u64>,
}

crud!(Topic {});

//分页查询 ,管理员可以查询hide的
impl_select_page!(Topic{select_page(kw:&str,c:u32,t:&str,login:bool) =>"
      ` where 1=1`
      if kw != '':
        ` and title like #{'%'+kw+'%'} 
      if c != 0:
        ` and tid=#{c}`
      if t != '':
        ` and tag like #{'%'+t+'%'}`
      if login==false:
        ` and hide = 0`
      if !sql.contains('count(1)'):
        ` order by ist desc,atime desc`"});

impl_select!(Topic{select_by_id(id:u64) => "`where id = #{id}`"});

impl_select!(Topic{select_cmp_latest(cur_id:u64,next:bool) => "
      `where 1=1`
      if next:
        ` and id > #{cur_id}`
      if next==false:
        ` and id < #{cur_id}`
      ` order by atime asc limit 1`
      "});

//查询所有标签
#[sql("select distinct tag from topic")]
pub async fn select_tags(rb: &RBatis) -> rbatis::Result<Vec<ValueMap>> {
    impled!()
}

//查询所有标题和时间
#[sql("select id,title,atime from topic where hide=0 order by atime desc")]
pub async fn select_sitemap(rb: &RBatis) -> rbatis::Result<Vec<Topic>> {
    impled!()
}

//查询热评文章
impl_select!(Topic{select_hots() => "`where num>0 order by num desc limit 10`"});

//删除分类时清空tid字段
impl_update!(Topic{clear_by_tid(tid:u64) => "`tid=null where tid=#{tid}`"});
